import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:jiyun_behavior/utils/app_theme.dart';
import 'package:jiyun_behavior/widgets/custom_button.dart';

class ConsultationScreen extends StatefulWidget {
  const ConsultationScreen({Key? key}) : super(key: key);

  @override
  State<ConsultationScreen> createState() => _ConsultationScreenState();
}

class _ConsultationScreenState extends State<ConsultationScreen> {
  String _selectedType = '';
  final _questionController = TextEditingController();
  final _expectationController = TextEditingController();
  String _selectedTimeframe = '短期内需要决定（1-2周内）';
  bool _isAgreed = true;

  @override
  void dispose() {
    _questionController.dispose();
    _expectationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('重要抉择咨询'),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                Center(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 10.0),
                    child: Text(
                      '在人生重要抉择时刻，我们将为您提供专业指引',
                      style: TextStyle(
                        color: AppTheme.subtitleColor,
                        fontSize: 14,
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 10),
                _buildConsultationTypes(),
                const SizedBox(height: 20),
                _selectedType.isNotEmpty
                    ? _buildConsultationForm()
                    : const SizedBox.shrink(),
                const SizedBox(height: 20),
                _buildConsultationHistory(),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildConsultationTypes() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '选择咨询类型',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            GridView.count(
              crossAxisCount: 2,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              mainAxisSpacing: 12,
              crossAxisSpacing: 12,
              childAspectRatio: 1.0,
              children: [
                _buildConsultationType(
                  title: '事业决策',
                  description: '职业规划、跳槽、创业',
                  icon: FontAwesomeIcons.briefcase,
                  color: AppTheme.primaryColor,
                  bgColor: AppTheme.secondaryColor,
                ),
                _buildConsultationType(
                  title: '感情抉择',
                  description: '婚恋、分合、缘分',
                  icon: FontAwesomeIcons.heart,
                  color: const Color(0xFF1890FF),
                  bgColor: const Color(0xFFE6F7FF),
                ),
                _buildConsultationType(
                  title: '财务决策',
                  description: '投资、理财、购房',
                  icon: FontAwesomeIcons.coins,
                  color: const Color(0xFF52C41A),
                  bgColor: const Color(0xFFF6FFED),
                ),
                _buildConsultationType(
                  title: '学业选择',
                  description: '专业选择、留学、考试',
                  icon: FontAwesomeIcons.graduationCap,
                  color: const Color(0xFFFA8C16),
                  bgColor: const Color(0xFFFFF7E6),
                ),
              ],
            ),
            const SizedBox(height: 16),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
              decoration: BoxDecoration(
                color: const Color(0xFFF9F9F9),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Row(
                children: [
                  const Icon(
                    Icons.search,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                  const SizedBox(width: 8),
                  Expanded(
                    child: TextField(
                      decoration: const InputDecoration(
                        hintText: '搜索其他咨询类型...',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                          fontSize: 14,
                          color: AppTheme.lightTextColor,
                        ),
                        isDense: true,
                        contentPadding: EdgeInsets.zero,
                      ),
                      style: const TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConsultationType({
    required String title,
    required String description,
    required IconData icon,
    required Color color,
    required Color bgColor,
  }) {
    final isSelected = _selectedType == title;
    
    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedType = title;
        });
      },
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.circular(12),
          border: isSelected
              ? Border.all(color: color, width: 2)
              : null,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Center(
                child: Icon(
                  icon,
                  size: 24,
                  color: color,
                ),
              ),
            ),
            const SizedBox(height: 12),
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              description,
              style: TextStyle(
                fontSize: 12,
                color: AppTheme.subtitleColor,
              ),
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConsultationForm() {
    return Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(color: AppTheme.primaryColor, width: 2),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                  width: 50,
                  height: 50,
                  decoration: BoxDecoration(
                    color: AppTheme.secondaryColor,
                    shape: BoxShape.circle,
                  ),
                  child: const Center(
                    child: Icon(
                      FontAwesomeIcons.briefcase,
                      size: 20,
                      color: AppTheme.primaryColor,
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '$_selectedType咨询',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        '根据您的命理基本盘，为您提供$_selectedType指引',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.subtitleColor,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),
            const Text(
              '您目前面临的抉择是什么？',
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            TextField(
              controller: _questionController,
              maxLines: 4,
              decoration: InputDecoration(
                hintText: '例如：我目前在A公司工作，收到了B公司的offer，是否应该跳槽？',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                contentPadding: const EdgeInsets.all(12),
              ),
            ),
            const SizedBox(height: 16),
            const Text(
              '您期望的结果是什么？',
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            TextField(
              controller: _expectationController,
              maxLines: 3,
              decoration: InputDecoration(
                hintText: '例如：希望能找到更适合自己发展的平台，提升收入和职位',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                contentPadding: const EdgeInsets.all(12),
              ),
            ),
            const SizedBox(height: 16),
            const Text(
              '您的决策时间范围',
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            Container(
              decoration: BoxDecoration(
                border: Border.all(color: AppTheme.dividerColor),
                borderRadius: BorderRadius.circular(8),
              ),
              child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                  value: _selectedTimeframe,
                  isExpanded: true,
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  items: [
                    '需要立即决定（1-3天内）',
                    '短期内需要决定（1-2周内）',
                    '中期规划（1-3个月内）',
                    '长期规划（半年以上）',
                  ].map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                  onChanged: (value) {
                    setState(() {
                      _selectedTimeframe = value!;
                    });
                  },
                ),
              ),
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  '咨询服务费用',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  '¥198.00',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: AppTheme.primaryColor,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                Checkbox(
                  value: _isAgreed,
                  activeColor: AppTheme.primaryColor,
                  onChanged: (value) {
                    setState(() {
                      _isAgreed = value!;
                    });
                  },
                ),
                Expanded(
                  child: RichText(
                    text: TextSpan(
                      text: '我已阅读并同意',
                      style: TextStyle(
                        fontSize: 14,
                        color: AppTheme.subtitleColor,
                      ),
                      children: [
                        TextSpan(
                          text: '《咨询服务协议》',
                          style: TextStyle(
                            color: AppTheme.primaryColor,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            CustomButton(
              text: '提交咨询并支付',
              onPressed: () {
                if (_questionController.text.isEmpty) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('请填写您面临的抉择'),
                      backgroundColor: Colors.red,
                    ),
                  );
                  return;
                }
                
                if (_expectationController.text.isEmpty) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('请填写您期望的结果'),
                      backgroundColor: Colors.red,
                    ),
                  );
                  return;
                }
                
                if (!_isAgreed) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('请阅读并同意咨询服务协议'),
                      backgroundColor: Colors.red,
                    ),
                  );
                  return;
                }
                
                // 提交咨询并支付逻辑
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(
                    content: Text('咨询提交成功，请等待专家回复'),
                    backgroundColor: AppTheme.primaryColor,
                  ),
                );
                
                // 清空表单
                setState(() {
                  _selectedType = '';
                  _questionController.clear();
                  _expectationController.clear();
                });
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConsultationHistory() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '历史咨询记录',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            _buildHistoryItem(
              title: '购房投资分析',
              content: '是否应该在当前城市购买第二套房产作为投资',
              date: '2023-05-20',
              status: '已完成',
            ),
            const Divider(),
            _buildHistoryItem(
              title: '职业发展规划',
              content: '未来3年的职业发展方向和提升计划',
              date: '2023-03-15',
              status: '已完成',
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildHistoryItem({
    required String title,
    required String content,
    required String date,
    required String status,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              status,
              style: TextStyle(
                fontSize: 14,
                color: const Color(0xFF52C41A),
              ),
            ),
          ],
        ),
        const SizedBox(height: 8),
        Text(
          '咨询内容：$content',
          style: TextStyle(
            fontSize: 14,
            color: AppTheme.subtitleColor,
          ),
        ),
        const SizedBox(height: 8),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              date,
              style: TextStyle(
                fontSize: 12,
                color: AppTheme.lightTextColor,
              ),
            ),
            GestureDetector(
              onTap: () {
                // 查看详情逻辑
              },
              child: Text(
                '查看详情',
                style: TextStyle(
                  fontSize: 12,
                  color: AppTheme.primaryColor,
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }
} 